﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RATSharp.Property;
using RATSharp.Collection;
using RATSharp.Validator;
namespace RATSharp
{

    delegate void PropertyHandler(PROPERTY propertyType);
    class PropertiesBrowser
    {
        Properties properties;

        public PropertiesBrowser(Properties properties)
        {
            this.properties = properties;

        }

        public void init()
        {
            PropertyLoader.PropertiesChangeEvent += setNewProperties;

            bool stepback = false;
            while (!stepback)
            {
                MenuList<MenuItem<string, PropertyHandler>, MenuItem<string, PropertyHandler>> preferenceList =
                     new MenuList<MenuItem<string, PropertyHandler>, MenuItem<string, PropertyHandler>>("Preferences");
                preferenceList.Add(new MenuItem<string, PropertyHandler>("Url: " + properties.url, new PropertyHandler(handleNewProperty)));
                preferenceList.Add(new MenuItem<string, PropertyHandler>("Port: " + properties.port, new PropertyHandler(handleNewProperty)));
                preferenceList.Add(new MenuItem<string, PropertyHandler>("Username: " + properties.username, new PropertyHandler(handleNewProperty)));
                preferenceList.Add(new MenuItem<string, PropertyHandler>("Password: xxx", new PropertyHandler(handleNewProperty)));
                preferenceList.Add(new MenuItem<string, PropertyHandler>("Path: " + properties.path, new PropertyHandler(handleNewProperty)));
                preferenceList.Add(new MenuItem<string, PropertyHandler>("Step Back", delegate(PROPERTY prop) { stepback = true; PropertyLoader.PropertiesChangeEvent -= setNewProperties; }));
                Console.WriteLine(preferenceList);

                int choice = MenuUtilities.GetSimpleInput(new IntRangeInputValidator(1, preferenceList.fullCount)) - 1;

                preferenceList.ElementAt(choice).menuHandler((PROPERTY)choice);
            }


        }


        private void handleNewProperty(PROPERTY propertyType)
        {
            Console.Write("Type new value: ");
            string value;
            switch (propertyType)
            {
                case PROPERTY.PATH:
                    Console.WriteLine("(path)");
                    value = MenuUtilities.GetSimpleInput(new StringInputValidator());
                    break;
                case PROPERTY.PASSWORD:
                    Console.WriteLine("(password)");
                    value = MenuUtilities.GetSimpleInput(new StringInputValidator());
                    break;
                case PROPERTY.PORT:
                    Console.WriteLine("(1-65535)");
                    value = Convert.ToString(MenuUtilities.GetSimpleInput(new IntRangeInputValidator(1, 65535)));
                    break;
                case PROPERTY.URL:
                    Console.WriteLine("(URL without protocol)");
                    value = MenuUtilities.GetSimpleInput(new URLInputValidator());
                    break;
                case PROPERTY.USERNAME:
                    Console.WriteLine("(username)");
                    value = MenuUtilities.GetSimpleInput(new StringInputValidator());
                    break;
                default:
                    return;
            }

            PropertyLoader.persist(propertyType, value, this);

        }

        private void setNewProperties(object o, Properties props)
        {
            this.properties = props;
        }

    }
}
